package com.ddv.www.sqlcipher.dbhelper;

import android.content.Context;

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;

/**
 * Created by 黄家三少 on 2017/10/12.
 */

public class DBCipherHelper extends SQLiteOpenHelper {

    //数据库名字
    private static final String DB_NAME = "huangxiaoguo_db";
    //数据库密码
    public static final String DB_PASSWORD = "huangxiaoguo";
    //数据库版本
    private static final int DB_VERSION = 1;

    //数据库表名称
    public static final String TABLE_NAME = "huangxiaoguo";

    //列名（字段一）
    public static final String FIELD_ID = "id";
    //列名（字段二）
    public static final String FIELD_NAME = "name";
    //列名（字段三）
    public static final String FIELD_NUM = "num";

    public DBCipherHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        //不可忽略的  进行so库加载
        SQLiteDatabase.loadLibs(context);
    }

    public DBCipherHelper(Context context) {
        this(context, DB_NAME, null, DB_VERSION);

    }

    /**
     * 创建数据库
     *
     * @param db
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建表
        createTable(db);
    }

    private void createTable(SQLiteDatabase db) {
        //CREATE TABLE person (personid integer primary key autoincrement, name varchar(20))
        // db.execSQL("create table stu(_id integer primary key autoincrement,name varchar(20),num varchar(20))");
        String sql = "create table " + TABLE_NAME + " (" +
                FIELD_ID + " integer primary key autoincrement," +
                FIELD_NAME + " text not null, " + FIELD_NUM + " varchar(20)" + ");";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //db.execSQL("alter table stu add sex varchar(20)");
        db.execSQL("alter table " + TABLE_NAME + " add phone varchar(12)"); //往表中增加一列
    }
}
